package com.wanxi.servlet.product;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.ProductModel;
import com.wanxi.service.ProductService;
import com.wanxi.service.impl.ProductServiceImpl;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/product/update")
public class ProductUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.得到前端的值
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String normalPrice = req.getParameter("normalPrice");
        String marketPrice = req.getParameter("marketPrice");
        String content = req.getParameter("content");
        String isShow = req.getParameter("isShow");
        String isRecommend = req.getParameter("isRecommend");
        String imgHref = req.getParameter("imgHref");
        String whereShow = req.getParameter("whereShow");
        String cuisineId = req.getParameter("cuisineId");
        String sort = req.getParameter("sort");
//        3.封装model
        ProductModel model = ProductModel.builder()
                .id(Tool.nullToInt(id))
                .name(Tool.nullToStr(name))
                .normalPrice(Tool.nullToDecimal(normalPrice))
                .marketPrice(Tool.nullToDecimal(marketPrice))
                .content(Tool.nullToStr(content))
                .isShow(Tool.nullToInt(isShow))
                .isRecommend(Tool.nullToInt(isRecommend))
                .imgHref(Tool.nullToStr(imgHref))
                .whereShow(Tool.nullToInt(whereShow))
                .cuisineId(Tool.nullToInt(cuisineId))
                .sort(Tool.nullToInt(sort))
                .build();
//        4.调用服务逻辑层
        ProductService productService = new ProductServiceImpl();
        ResultDTO resultDTO = productService.update(model);
//        5.返回给前端
        resp.getWriter().println(JSONObject.toJSONString(resultDTO));
    }
}
